const { app, BrowserWindow } = require("electron");
const path = require("path");
const WinState = require("electron-win-state").default;
//获取图片
require("./controller/getSource");
//警告弹窗
require("./controller/alert");
//打开窗口
require("./controller/openWindow");
//获取文件列表
require("./controller/getFiles");
//菜单
require("./controller/buildMenu");
//后台图标
const createTray = require("./controller/tray");

const createWindow = () => {
  const winState = new WinState({
    defaultWidth: 800,
    defaultHeight: 600,
    electronStoreOptions: {
      name: "window-state-main",
    },
  });

  const win = new BrowserWindow({
    ...winState.winOptions,
    webPreferences: {
      preload: path.join(__dirname, "./preload/index.js"),
    },
    show: false,
  });

  //dev环境
  win.loadURL("http://localhost:5173/");

  const wc = win.webContents;

  //开发者工具
  wc.openDevTools();

  //等待页面加载完成之后打开窗口
  win.on("ready-to-show", () => {
    win.show();
  });

  //创建后台图标
  createTray(app, win);

  //窗口状态记录
  winState.manage(win);
};

app.whenReady().then(() => {
  createWindow();

  //在mac上没有窗口则打开一个窗口
  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

//在Windows和Linux上，关闭所有窗口退出
app.on("window-all-closed", () => {
  if (process.platform !== "darwin") app.quit();
});
